package com.ironman.compute.generator;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.event.threadpool.ThreadPool;
import org.noear.snack.ONode;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

public abstract class ComputeDataGenerator {

    public void start(String mHmId, BiConsumer<String, String> biConsumer) {
        ThreadPool.SCHEDULED_EXECUTOR.scheduleAtFixedRate(() -> {
            ONode node = ONode.newObject()
                    .set("hmId", mHmId)
                    .set("ts", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN))
                    .set("data", generate());
            biConsumer.accept(topicType(), node.toJson());
        }, 0, 1, TimeUnit.SECONDS);
    }

    abstract String topicType();

    abstract ONode generate();
}
